var names=$.cookie("username")?JSON.parse($.cookie("username")):[];
var huanying='';
if($.cookie("username")){
    huanying=`<div class="dc fl">欢迎${names}</div>`;
    $('.fr .dc').html(huanying);
}

//保存下标

//页面刷新自动执行定时器


//点击左箭头显示上一张图片



function banner(){
    var current_index=0;
    //保存定时器
    var timed=null;
    //找到所有img
    var imgs=$('.m-banner .swiper img');
    //找到所有span
    var spaner=$('.m-banner .yuandian span');
    //左箭头
    autoPlay()
$('.m-banner .left').on('click',function(){
    console.log('我被点了');
    current_index--;
    if(current_index<0){
        current_index=imgs.length-1;
    }
    changePic();
})
//点击右箭头显示上一张图片
$('.m-banner .right').on('click',function(){
    console.log('我被点了');
    current_index++;
    if(current_index>imgs.length-1){
        current_index=0;
    }
    changePic();
})

spaner.on('click',function(){
   current_index=$(this).index();
   changePic()
})
//移入删除定时器
$('.m-banner .swiper').on('mouseover',function(){
    clearInterval(timed)
})
//鼠标移出执行自动轮播
$('.m-banner .swiper').on('mouseout',function(){
    autoPlay()
})
//自动轮播
function autoPlay(){
    timed = setInterval(function(){
        current_index++;
        if( current_index > imgs.length - 1 ){
            current_index = 0;
        }
        changePic();
    },2000)
}

//切换图片
function changePic(){
    //让当前显示图片隐藏  
     imgs.parent().children('.active').removeClass('active');
    //让下一张图片显示
    imgs.eq(current_index).addClass('active');
    
    //让当前高亮小圆点 取消高亮
    spaner.parent().children('.activ').removeClass('activ');
    //让对应位置的小圆点高亮
    spaner.eq(current_index).addClass('activ');
}

}
//鼠标移入销毁定时器

var index_current=0;

//找到秒杀下面的smallbox
var smallbox=$('.miaoshaa .bigbox .smallbox');
//添加点击事件
$('.miaoshaa .bigbox .left').on('click',function(){
    
})
$('.miaoshaa .bigbox .right').on('click',function(){
    
})
var min=$('.xinpin .bigbox .smallbox .min');
var smallbo=$('.xinpin .bigbox .smallbox');
var index_current=0;
//添加点击事件
$('.xinpin .bigbox .left').on('click',function(){
    
})
$('.xinpin .bigbox .right').on('click',function(){
    
   
   
})
//有品秒杀倒计时
//创建20点时间
// var endyear=new Date().getFullYear();
// var endmoth=new Date().getMonth()+1;
// var endday= new Date().getDate();
var endtime=new Date('2022-5-30 20:00:00')
// console.log(endyear,endmoth,endday);
var timerd=setInterval(function(){
    var now=new Date();
    
    //计算结束事件和当前最新事件的差值
    var time =endtime-now;
    //倒计时结束销毁定时器
    if(time<=0){
        clearInterval(timerd);
    }else{
        //算出time中有多少分钟
        var hour=parseInt(time/(1*60*60*1000))%24;
        //算出time中剩余多少时间  假设时间一共78分钟 60取余 都得到18分钟
        var minute =parseInt(time/(1*60*1000))%60;
        var second=parseInt(time/(1*1000))%60;
        // console.log(hour,minute,second)
        var spanss=$('.miaosha .container .top .div .miao');
        spanss[0].innerHTML=buling(hour);
        spanss[1].innerHTML=buling(minute);
        spanss[2].innerHTML=buling(second);
        
    }
},1000)
//补0
function buling(num){
    return num<10?"0"+num:""+num;
}

let str='';
let spans='';
$.get('bannerGet.php',function(data){
  
    console.log(data);
    var res=JSON.parse(data);
    res.forEach(item=>{
        str+=`<a href=""><img src="${item.img}" alt=""></a>`;
        spans+=`<span></span>`;
        })
     $('.banner-box .m-banner .swiper').html(str);
     $('.banner-box .m-banner .yuandian').html(spans);  
     $('.banner-box .m-banner .swiper img').eq(0).addClass('active');
     $('.banner .banner-box .m-banner .yuandian span').eq(0).addClass('activ');
        banner();
})
let xdd='';
$.get('../html/getGoodsList.php',{typeId:"002"},function(data){
    var caini=JSON.parse(data);
    caini.forEach(item=>{
        xdd+=`<div class="box" onclick='location.href="./商品详情页.html?goodsid=${item.goodsId}"'>
        <div class="img">
            <div class="imgbox">
                <img src="${item.goodsImg}" alt="">
            </div>
            <p>${item.goodsDesc}</p>
        </div>
        <div class="write">
            <div class="imgs">
                
            </div>
            <p>${item.goodsName}</p>
            <p><span>￥</span><span class="s1">${item.goodsPrice}</span><span>起</span> </p>
        </div>
    </div>`;
    })
    $('.foottop .list').html(xdd);
 
})
let leibie='';
$.get('../html/getGoodsType.php',function(data){
    var fenleichuangcao=JSON.parse(data);

    fenleichuangcao.forEach(item=>{
        leibie+=`<div class="chuangzao" onclick='location.href="./商品栏界面.html?typesid=${item.typeId}"'>${item.goodsType}</div>`
    })
    $('.fenleishangpin').html(leibie);
    
    console.log(data);
    console.log(leibie);
})
